<template>
  <div class="app-container">
    <div class="view-title">余额记录</div>
    <el-row type="flex" align="middle">
      <el-col :span="1">
        <div class="">用户id</div>
      </el-col>
      <el-col :span="3">
        <el-input v-model="userIdValue" placeholder="请输入用户id"></el-input>
      </el-col>
      <el-col :span="1" class="search_title">
        <div class="">订单</div>
      </el-col>
      <el-col :span="3">
        <el-input v-model="orderNoValue" placeholder="请输入订单号"></el-input>
      </el-col>
      <el-col :span="1" class="search_title">
        <div class="">订单来源</div>
      </el-col>
      <el-col :span="3" class="search_source">
        <el-select v-model="sourceValue" placeholder="请选择订单来源">
          <el-option
            v-for="item in sourceOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <!-- <el-input v-model="orderNoValue" placeholder="请输入订单号"></el-input> -->
      </el-col>
      <el-col :span="2">
        <el-button type="primary" @click="handleSearch" class="search_btn"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="6"> 总分销佣金： {{ sumBalance }}元 </el-col>
    </el-row>

    <el-row type="flex" align="middle">
      <el-col :span="2">
        <el-button
          type="warning"
          size="mini"
          @click="getAppUserBalanceRecordListExport"
          class="export_btn"
          >导出余额记录</el-button
        >
      </el-col>
    </el-row>

    <el-table
      :data="recordTableData"
      v-loading="loading"
      element-loading-text="正在加载中"
      :header-cell-style="{
        borderColor: '#F0F0F0',
        height: '51px',
        background: '#F0F0F0',
        color: '#333',
      }"
      :row-style="{ height: '51px' }"
      :cell-style="{ borderColor: '#F0F0F0' }"
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="user_id" label="用户id" align="center">
      </el-table-column>
      <el-table-column prop="balance" label="余额" align="center">
      </el-table-column>
      <el-table-column prop="source" label="余额来源" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.source == 1">合伙人分佣</span>
          <span v-else-if="scope.row.source == 2">商城订单</span>
          <span v-else-if="scope.row.source == 3">充值</span>
          <span v-else-if="scope.row.source == 4">分享者分佣</span>
          <span v-else-if="scope.row.source == 5">提现</span>
          <span v-else-if="scope.row.source == 6">活动奖金</span>
        </template>
      </el-table-column>
      <el-table-column prop="landing_order_no" label="订单" align="center">
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" align="center">
      </el-table-column>
      <el-table-column prop="updated_at" label="更新时间" align="center">
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="page_box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { getAppUserBalanceRecordList } from "@/api/balanceRecord.js";

export default {
  data() {
    return {
      pageNow: 1,
      pageSize: 10,
      total: null,
      recordTableData: [],
      loading: true,
      userIdValue: "",
      sumBalance: "",
      orderNoValue: "",
      sourceValue: "",
      sourceOptions: [
        {
          value: "1",
          label: "合伙人分佣",
        },
        {
          value: "2",
          label: "商城订单",
        },
        {
          value: "3",
          label: "充值",
        },
        {
          value: "4",
          label: "分享者分佣",
        },
        {
          value: "5",
          label: "提现",
        },
        {
          value: "6",
          label: "活动奖金",
        },
      ],
    };
  },
  mounted() {
    this.getAppUserBalanceRecordList();
  },
  methods: {
    // 搜索
    handleSearch() {
      this.getAppUserBalanceRecordList();
    },

    // 全部余额记录
    getAppUserBalanceRecordList() {
      getAppUserBalanceRecordList({
        source: this.sourceValue,
        user_id: this.userIdValue,
        landing_order_no: this.orderNoValue,
        page_now: this.pageNow,
        page_size: this.pageSize,
      }).then((res) => {
        if (res.code == 1) {
          this.loading = false;
          this.recordTableData = res.data;
          this.sumBalance = res.sum_balance;
          this.total = res.page_info.total_count;
        } else {
          this.loading = false;
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 全部余额记录-导出
    getAppUserBalanceRecordListExport() {
      getAppUserBalanceRecordList({
        source: this.sourceValue,
        user_id: this.userIdValue,
        landing_order_no: this.orderNoValue,
        page_now: this.pageNow,
        page_size: this.pageSize,
        is_export: 1,
      }).then((res) => {
        if (res.code == 1) {
          location.href = res.data.file_path;
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageNow = 1;
      this.getAppUserBalanceRecordList();
    },
    handleCurrentChange(val) {
      this.pageNow = val;
      this.pageSize = 10;
      this.getAppUserBalanceRecordList();
    },
  },
};
</script>
<style lang="scss" scoped>
.search_btn {
  margin-left: 10px;
}

.export_btn {
  margin-top: 20px;
}

.search_title {
  margin-left: 20px;
}

.search_source {
  margin-left: 10px;
}
</style>
